# game_loop.py

import game_defs

def game_loop():
    command = input('请输入您的指令（例如：落子：p3D，移动棋子：m3D4E，吃子：c4E）：')

    action = command[0]
    # 将输入的坐标转换为大写字母
    command = command.upper()

    if action == 'P' or action == 'p':
        if len(command) != 3:
            raise ValueError('无效的落子指令')
        row = int(command[1]) - 1
        col = ord(command[2].upper()) - ord('A')
        if not game_defs.place_piece(row, col, 'X' if action == 'P' else 'O'):
            raise ValueError('落子失败')
    elif action == 'M' or action == 'm':
        if len(command) != 5:
            raise ValueError('无效的移动棋子指令')
        src_row = int(command[1]) - 1
        src_col = ord(command[2].upper()) - ord('A')
        dst_row = int(command[3]) - 1
        dst_col = ord(command[4].upper()) - ord('A')
        if not game_defs.move_piece(src_row, src_col, dst_row, dst_col):
            raise ValueError('移动棋子失败')
    elif action == 'C' or action == 'c':
        if len(command) != 3:
            raise ValueError('无效的吃子指令')
        row = int(command[1]) - 1
        col = ord(command[2].upper()) - ord('A')
        if not game_defs.capture_piece(row, col):
            print('吃子失败')
    else:
        print('无效的指令')
        
